package cn.zhuizikejithreeig.com.passenger;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public class SelectActivity extends BasicActivity implements View.OnClickListener {
    private Button mFirstModle;
    private Button mSecondModle;
    private Button mThireModle;
    private WebSocketClient mWebSocketClient;
    private String address = "wss://bs.3igtech.com";
    private URI uri;
    @Override
    protected boolean actionBarNeedGoon() {
        return false;
    }

    @Override
    public int setContView() {
        return R.layout.activity_select;
    }

    @Override
    protected int setHandIndexShow() {
        return 2;
    }

    @Override
    protected void initToDo() {
        setTitle("选择服务");
        setRelatLeftShow(1, R.mipmap.back, "");
        getRelatLift().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        try {
            uri = new URI(address);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        //开启socket
        startSocket();
    }

    private void startSocket() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (null == mWebSocketClient) {
                    mWebSocketClient = new WebSocketClient(uri) {
                        @Override
                        public void onOpen(ServerHandshake serverHandshake) {
                            System.out.println("开启了连接");
                        }
                        @Override
                        public void onMessage(String s) {
                            System.out.println("发送了信息");
                        }
                        @Override
                        public void onClose(int i, String s, boolean b) {
                            System.out.println("关闭了连接");
                        }
                        @Override
                        public void onError(Exception e) {
                            System.out.println("连接出错了");
                        }
                    };
                    mWebSocketClient.connect();
                }
            }
        }).start();
    }

    @Override
    protected void initView() {

        mFirstModle = (Button) findViewById(R.id.firstmodle);
        mSecondModle = (Button) findViewById(R.id.secondmodle);
        mThireModle = (Button) findViewById(R.id.thiredmodle);

        mThireModle.setOnClickListener(this);
        mSecondModle.setOnClickListener(this);
        mThireModle.setOnClickListener(this);
    }

    @Override
    protected void netChengedListen(int netMobile) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.firstmodle:
                startSocket();
                break;
            case R.id.secondmodle:
                startSocket();
                break;
            case R.id.thiredmodle:
                startSocket();
                break;
        }
    }
}
